<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * @author Administrator
 *
 */
class Utils extends CI_Controller
{

    /**
     */
    function __construct()
    {
        parent::__construct();
    }

    function phpinfo()
    {
        echo phpinfo();
        die();
    }

    function gmtime($gmtime = '')
    {
        $gmtime = empty($gmtime) ? gmtime() : $gmtime;
        echo local_date('Y-m-d H:i:s', $gmtime);
        die();
    }

    /**
     * 生成验证码
     * @param string $code_type
     */
    public function captcha($code_type = 'signin')
    {
        $this->common_model->captcha($code_type);
    }


    /**
     * 批量文件上传接口
     * @param string $class
     */
    public function upload()
    {
        $content     = '';
        $message     = 'ok';
        $append_data = array();
        $post        = $this->input->post();
        $action      = trim($post['action']);
        $item_id     = isset($post['itemid']) ? intval($post['itemid']) : 0;
        $sessid      = isset($post['sessid']) ? trim($post['sessid']) : '';
        //格式化图片路径
        $image_path = reformat_image_name($item_id, $action, $_FILES['Filedata']['name']);

        move_upload_file($_FILES['Filedata']['tmp_name'], full_static_path($image_path));

        switch ($action) {
            case 'helper_gallery':
                $this->load->model('helper_model');
                $result      = $this->helper_model->upload_gallery($item_id, $image_path);
                $append_data = $result['data'];
                break;
            default:
                $result['error']   = 300;
                $result['message'] = '非法操作！[' . $action . ']';
                break;
        }
        $append_data['filePath'] = full_static_url($image_path);
        $append_data['action']   = $action;//必选字段
        if ($result['error'] > 0) {
            make_json_error($result['message'] . "\r\n[Err:" . $result['error'] . "]");
        } else {
            make_json_result($content, $message, $append_data);
        }
    }


    /**
     * 发送短消息
     * @param unknown $mobile
     * @param unknown $code_type
     */
    public function send_signup_sms($mobile)
    {
        $this->send_sms($mobile, 'signup');
    }

    private function send_sms($mobile, $code_type = 'signup')
    {
        $return_code = $this->common_model->send_sms($mobile, $code_type);
        if ($return_code == 1) {
            make_json_result($mobile, 'ok');
        } else {
            make_json_error("发送失败，请稍后重试！\r\n[Err:$return_code	]");
        }
    }

    /**
     * 校验短信验证码
     * @param unknown $sms_code
     * @param string $code_type
     */
    public function check_signup_sms($sms_code)
    {
        $this->verify_sms($sms_code, 'signup');
    }

    private function verify_sms($sms_code, $code_type = 'signup')
    {
        $return_code = $this->common_model->verify_sms($sms_code, $code_type);
        switch ($return_code) {
            case 200:
                make_json_result($sms_code, 'ok');
            case 300:
                $serve_code = $this->session->userdata('sms_code');
                make_json_error("验证码不正确！" . $serve_code);
            case 400:
            default:
                make_json_error("验证码无效，请重新获取！");
                break;
        }
    }

    public function gps_correct()
    {
        $gps    = $_REQUEST['gps'];
        $coords = explode(',', $gps);
        //高德gps纠偏
        $this->load->library('third_party/gps_correct');
        $correct_location              = $this->gps_correct->transform($coords[0], $coords[1]);
        $append_data['correct_coords'] = implode(',', $correct_location);
        $append_data['lng']            = $correct_location[0];
        $append_data['lat']            = $correct_location[1];
        make_json_result('', 'ok', $append_data);
    }

    public function send_post()
    {
        $post          = $this->input->post();
        $remote_server = (!isset($post['url']) || empty($post['url'])) ? '' : urldecode($post['url']);
        $post_data     = (!isset($post['post_data']) || empty($post['post_data'])) ? '' : $post['post_data'];
        $post_array    = isset($post['post_data']) ? json_decode($post['post_data'], TRUE) : '';
// 		make_json_error($remote_server.'请求地址错误！12'.json_encode($post_array));
        if (empty($remote_server)) {
            make_json_error('请求地址错误！');
        }
        $result = $this->request_by_curl($remote_server, $post_array);

        die($result);
    }

    /**
     * Curl版本
     * 使用方法：
     * $post_string = "app=request&version=beta";
     * request_by_curl('http://www.qianyunlai.com/restServer.php', $post_array);
     */
    function request_by_curl($remote_server, $post_array = array())
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, "CURL Example beta");
        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }

    /**
     * 生成随机的字符串(已去除0 o O )
     *    $length 长度
     *    $type 类型：all;(num lower upper可以组合)
     * @return string
     */
    public function random_key($length = 6, $type = 'num')
    {
        return make_json_result(random_str($length, $type));
    }

    public function send_test_sms($mobiles = '')
    {
        if (empty($mobiles)) {
            die('Any number not found...');
        }
        $this->load->library('third_party/sms_sioo');
        $now = local_date('Y-m-d H:i:s');
        echo $now . '<br>';
        $msg    = '【测试短信】' . $now . ' => ' . $mobiles;
        $result = $this->sms_sioo->send($mobiles, $msg);
        echo($result);
        die();
    }

    /**
     * 解密卡号
     * @return [type] [description]
     */
    public function decode_card()
    {
        $post = $this->input->post();

        if (empty($post['card'])) {
            make_json_error('请求参数错误');
        }
        // 默认是读取物理卡号，否则是读取扇区内容
        if (empty($_SESSION['facility_card_info'])) {
            $card = getCardNumByPhysicalNum($post['card']);
        } else {
            $card = decode($post['card']);
        }
        if (empty($card)) {
            make_json_error('该卡不存在');
        } else {
            make_json_result($card, 'ok');
        }
    }

}

?>